def split_text_by_length(text, max_len=300):
    sentences = [s.strip() for s in text.replace('\n', '').split('。') if s.strip()]
    segments = []
    current = ""
    for s in sentences:
        s_full = s + "。"
        if len(current) + len(s_full) <= max_len:
            current += s_full
        else:
            if current:
                segments.append(current)
            current = s_full
    if current:
        segments.append(current)
    return segments

def detect_encoding(fname):
    for enc in ("utf-8", "gb2312"):
        try:
            with open(fname, "r", encoding=enc) as f:
                f.read()
            return enc
        except Exception:
            continue
    return None